package cn.silver.framework.mybatis.domain;

import cn.silver.framework.core.enums.SystemStatusEnums;
import cn.silver.framework.core.utils.DateUtils;
import cn.silver.framework.satoken.model.LoginUser;
import cn.silver.framework.satoken.utils.LoginHelper;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @author zhouxiaobo
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(name = "BusEntity", description = "业务实体")
public class BusEntity extends BaseEntity {
    /**
     * 业务编码
     */
    @Schema(title = "业务编码")
    private String code;

    /**
     * 业务名称
     */
    @Schema(title = "业务名称")
    private String name;
    /**
     * 业务状态
     */
    @Schema(title = "当前状态")
    private String status;
    /**
     * 序号
     */
    @Schema(title = "序号")
    private Integer sort;
    /**
     * 创建部门
     */
    @Schema(title = "备注说明")
    private String remark;
    /**
     * 创建部门
     */
    @Schema(title = "创建单位")
    private String createDept;

    /**
     * 创建者
     */
    @Schema(title = "创建者")
    private String createBy;

    /**
     * 创建时间
     */
    @Schema(title = "创建时间")
    @JsonFormat(timezone = DateUtils.TIME_ZONE, pattern = DateUtils.YYYY_MM_DD_HH_MM_SS)
    private Date createTime;

    /**
     * 更新者
     */
    @Schema(title = "更新者")
    private String updateBy;

    /**
     * 更新时间
     */
    @Schema(title = "更新时间")
    @JsonFormat(timezone = DateUtils.TIME_ZONE, pattern = DateUtils.YYYY_MM_DD_HH_MM_SS)
    private Date updateTime;

    /**
     * 删除标志
     */
    @TableLogic(delval = "1", value = "0")
    @Schema(title = "删除标志")
    private Boolean delFlag;

    @Override
    public void preInsert() {
        super.preInsert();
        if (StringUtils.isBlank(this.status)) {
            this.status = SystemStatusEnums.NORMAL.getCode();
        }
        this.createTime = DateUtils.getNowDate();
        this.updateTime = DateUtils.getNowDate();
        try {
            LoginUser user = LoginHelper.getLoginUser();
            if (user != null) {
                this.createBy = user.getId();
                this.updateBy = user.getId();
                this.createDept = user.getDeptId();
            }
        } catch (Exception e) {
            log.error("获取登录信息失败，{}", e.getMessage(), e);
        }

    }

    @Override
    public void preUpdate() {
        super.preUpdate();
        this.updateTime = DateUtils.getNowDate();
        LoginUser user = LoginHelper.getLoginUser();
        if (user != null) {
            this.updateBy = user.getId();
        }
    }

}
